﻿using Common;
using Common.XmlHandle;
using Network.UpdateData;
using SFJDManagement.SocketHandle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SFJDManagement
{
    /// <summary>
    /// Page_Edit_Brigade.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Edit_Brigade : Page
    {
        DataGrid dataGrid = null;
        string preValue = "";

        public Page_Edit_Brigade()
        {
            InitializeComponent();
            Ini();
        }

        private void Ini()
        {
            //载入数据
            List<ClassBrigade> list = Runtime.xmlBrigade.GetAll();
            dataGrid = new DataGrid();
            dataGrid.ItemsSource = list;
            //dataGrid.SetBinding(, UpdateSourceTrigger.PropertyChanged);
            dataGrid.CanUserAddRows = false;
            dataGrid.CanUserDeleteRows = false;
            dataGrid.CanUserReorderColumns = false;
            //Grid_Layout.Children.Add(dataGrid);
            Content = dataGrid;
            dataGrid.Loaded += DataGrid_Loaded;
            dataGrid.BeginningEdit += DataGrid_BeginningEdit;
            dataGrid.CellEditEnding += DataGrid_CellEditEnding;
        }

        private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            preValue = (e.Column.GetCellContent(e.Row) as TextBlock).Text;
        }

        private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            string value = (e.Column.GetCellContent(e.Row) as TextBox).Text;
            if (value == preValue)
                return;
            ClassBrigade brigade = new ClassBrigade();
            foreach(DataGridColumn column in dataGrid.Columns)
            {
                string str = "";
                try
                {
                    str = (column.GetCellContent(e.Row) as TextBlock).Text;
                }
                catch
                {
                    str = (column.GetCellContent(e.Row) as TextBox).Text;
                }
                //System.Diagnostics.Debug.Print(str);
                switch(column.Header as string)
                {
                    case "Guid":
                        brigade.Guid = new Guid(str);
                        break;
                    case "FullName":
                        brigade.FullName = str;
                        break;
                    case "District":
                        brigade.District = str;
                        break;
                    case "City":
                        brigade.City = str;
                        break;
                    case "Brigade":
                        brigade.Brigade = str;
                        break;
                    case "Code":
                        brigade.Code = str;
                        break;
                    case "Address":
                        brigade.Address = str;
                        break;
                    case "Province":
                        brigade.Province = str;
                        break;
                    case "Postcode":
                        brigade.Postcode = str;
                        break;
                }
            }
            Packet packet = new Packet(typeof(SC02EditBrigade).GetClient2ServerKey(), Runtime.config.UserGuid, brigade.Coding2String());
            CSendMsg.SendMsgNoWait(packet.ToBytes());
        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            foreach(DataGridColumn column in dataGrid.Columns)
            {
                switch(column.Header as string)
                {
                    case "Guid":
                        column.IsReadOnly = true;
                        break;
                    case "Code":
                        column.IsReadOnly = true;
                        break;
                }
            }
        }

        private void GetCellBrigade()
        {

        }
    }
}
